package kr.hkit.android_bookmgr.adapter;

import java.util.ArrayList;
import java.util.StringTokenizer;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import kr.hkit.android_bookmgr.R;
import kr.hkit.android_bookmgr.DTO.BookData;
import kr.hkit.android_bookmgr.util.ImageUtil;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchAdapter extends ArrayAdapter<BookData> { // 네이버 api - 사용

	private ArrayList<BookData> data;

	private View v;
	private ImageView image;
	private Context context;
	
	public SearchAdapter(Context context, int textViewResourceId, ArrayList items) {
		super(context, textViewResourceId, items);
		this.context = context;
		this.data = items;
	}
	

	public View getView(int position, View convertView, ViewGroup parent) {

		v = convertView;

		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.addbook_search_row, null);
		}

		BookData item = data.get(position);
		
		String resultName = removeB(item.name);
		String resultAuthor = removeB(item.author); //<b>태그 제거
		String resultPublisher = removeB(item.publisher); 

		if (item != null) {
			image = (ImageView) v.findViewById(R.id.addbook_search_img);
			String str = item.bookimg;
			ImageUtil.imageLoader(context);
			DisplayImageOptions options = ImageUtil.dispalyOptions();
			ImageLoader.getInstance().displayImage(str, image, options); //http주소만 출력할거니까 
			
			TextView v_name = (TextView) v.findViewById(R.id.addbook_search_name);
			TextView v_author = (TextView) v.findViewById(R.id.addbook_search_author);
			TextView v_publisher = (TextView) v.findViewById(R.id.addbook_search_publisher);
			TextView v_price = (TextView) v.findViewById(R.id.addbook_search_price);

			v_name.setText("도서명 : " + resultName); 
			v_author.setText("저자 : " + resultAuthor);
			v_publisher.setText("출판사 : " + resultPublisher);
			v_price.setText("가격 : " + item.price + "원");
			
		}
		return v;
	}
	
	private String removeB(String b_name){
		Log.d("b_name", b_name);
		
		StringTokenizer stk = new StringTokenizer(b_name,"</b>");
		String remove ="";
		for(int i=0; stk.hasMoreTokens(); i++){
			 remove += stk.nextToken();
		}
		return remove;
	}  
	
}
